/** 
 * @(#)RoutingTableRIP.java
 */

/**
 * @author Didier Gutacker
 * @version 1.0
 * @see Table
 * @see RoutingTableWindowRIP 
 *
 */

import java.util.*;
import java.util.Vector;
import java.awt.Frame;

public class RoutingTableRIP extends Table {
  
  public RIPRoutingTableWindow tableWindow;
 
  public RoutingTableRIP() {
    super();
  } // RoutingTableRIP.RoutingTableRIP

  public boolean update( int destinationID, int metric, LinkRIP l, int infinity ) {
    entryChangeGuard.acquire();
    RoutingEntry entry = lookUp( destinationID );

    if ( entry == null ) {
      entries.addElement( new RoutingEntry( destinationID, metric, l, infinity ) );
      entryChangeGuard.release();
      return true;
    } else {
      boolean returnValue = entry.update( destinationID, metric, l, infinity );
      entryChangeGuard.release();
      return returnValue;
    } // if
  } // RoutingTableRIP.update

  public void removeEntries( int destinationID ) {
    entryChangeGuard.acquire();
    for ( int i = 0; i < entries.size(); i += 1 ) {
      if ( ( (RoutingEntry) entries.elementAt( i ) ).destinationID == destinationID ) {
	entries.removeElementAt( i );
	break;
      } // if
    } // for
    entryChangeGuard.release();
  } // RoutingTableRIP.removeEntry

  public void removeEntries( LinkRIP l ) {
    entryChangeGuard.acquire();
    for ( int i = 0; i < entries.size(); i += 1 ) {
      if ( ( (RoutingEntry) entries.elementAt( i ) ).l.equals( l ) ) {
	entries.removeElementAt( i );
      } // if
    } // for
    entryChangeGuard.release();
  } // RoutingTableRIP.removeEntry

  public RoutingEntry lookUp( int destinationID ) {
    entryChangeGuard.acquire();
    for ( int i = 0; i < entries.size(); i += 1 ) {
      if ( ( (RoutingEntry) entries.elementAt( i ) ).destinationID == destinationID ) {
	entryChangeGuard.release();
	return (RoutingEntry) entries.elementAt( i );
      } // if
    } // for
    entryChangeGuard.release();
    return null;
  } // RoutingTableRIP.lookUp

  public void updateTableWindow() {
    if ( tableWindow != null ) {
      tableWindow.repaint();
    } // if
  } // RoutingTableRIP.updateTableWindow
  
  public void tableWindowDone() {
    tableWindow = null;
  } // RoutingTableRIP.tableWindowDone
      
} // RoutingTableRIP
